package com.pospi.pai.yunpos.vip;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lany.sp.SPHelper;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.pospi.callbacklistener.HttpCallBackListener;
import com.pospi.dto.VipBeen;
import com.pospi.http.HttpConnection;
import com.pospi.pai.yunpos.R;
import com.pospi.pai.yunpos.base.Base2Activity;
import com.pospi.pai.yunpos.login.Constant;


import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

public class VIPFavourActivity extends Base2Activity implements View.OnClickListener {
    @Bind(R.id.tv_search)
    ImageView tv_search;
    @Bind(R.id.et_vipNumber)
    EditText et_vipNumber;
    @Bind(R.id.vip_list)
    ListView vip_list;
    private List<VipBeen> beens = new ArrayList<>();
    private MyAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_vipfavour);
        setTitle("会员查询");
        ButterKnife.bind(this);
        tv_search.setOnClickListener(this);
        adapter = new MyAdapter();
        vip_list.setAdapter(adapter);
        vip_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(VIPFavourActivity.this, VipSalesSearchActivity.class);
                intent.putExtra("data", beens.get(i));
                startActivity(intent);

            }
        });
        init();
    }

    private void init() {

    }


    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tv_search) {
            search();
        }
    }
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    closeMyDialog();
                    if (beens.size() == 0) {
                        Toast.makeText(VIPFavourActivity.this, "未查询到数据", Toast.LENGTH_SHORT).show();
                    } else {
                        adapter.notifyDataSetChanged();
                    }
                    break;
                case 3:
                    closeMyDialog();
                    Toast.makeText(VIPFavourActivity.this, "服务器异常，请稍后重试", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    VipBeen vipBeen;
    private void search() {
        showMyDialog("查询中...");
        beens.clear();
        //查询会员
        Map<String,String> params = new HashMap<>();
        params.put("logid", SPHelper.getInstance().getString(Constant.USER_ID));
        params.put("sysid", SPHelper.getInstance().getString(Constant.SYS_ID));
        params.put("model", "vip.Vvip");
        params.put("fun", "find");
        params.put("table", "vip_info");

        JSONObject object = new JSONObject();
        try {
            object.put("filter", et_vipNumber.getText().toString().trim());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        params.put("pds", object.toString());
        new HttpConnection().postNet(params, new HttpCallBackListener() {
            @Override
            public void CallBack(String Response) {
                Log.i("vip", Response);
                try {
                    JSONObject object = new JSONObject(Response);
                    String code = object.getString("errCode");
                    JSONArray array = object.getJSONArray("result");
                    List<VipBeen> vipBeens = new Gson().fromJson(array.toString(), new TypeToken<List<VipBeen>>() {
                    }.getType());
                    beens.addAll(vipBeens);
                    Message message = Message.obtain();
                    message.what = 1;
                    handler.sendMessage(message);



                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

    }
    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return beens.size();
        }

        @Override
        public Object getItem(int i) {
            return beens.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            Holder holder;
            if (view == null) {
                holder = new Holder();
                view = LayoutInflater.from(VIPFavourActivity.this).inflate(R.layout.activity_vip_litem, null);
                holder.hym = view.findViewById(R.id.hym);
                holder.hyh = view.findViewById(R.id.hyh);
                holder.jfye = view.findViewById(R.id.jfye);
                holder.mzye = view.findViewById(R.id.mzye);
                holder.hylx = view.findViewById(R.id.hylx);
                view.setTag(holder);
            } else {
                holder = (Holder) view.getTag();
            }
            holder.hym.setText(beens.get(i).getName());
            holder.hyh.setText(beens.get(i).getNum());
            holder.jfye.setText(beens.get(i).getJfye());
            holder.mzye.setText(beens.get(i).getMzye());

            return view;
        }
    }
    class Holder{
        TextView hym;
        TextView hyh;
        TextView jfye;
        TextView mzye;
        TextView hylx;
    }

}
